package org.bogusware.json.editor;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.swt.SWT;

public class JSONObjectScanner extends RuleBasedScanner {

	public JSONObjectScanner(ColorManager manager) {
		IToken string = new Token(new TextAttribute(manager.getColor(IJSONColorConstants.STRING), null, SWT.BOLD));
		IToken constant = new Token(new TextAttribute(manager.getColor(IJSONColorConstants.JSON_CONSTANT), null, SWT.BOLD));
		IToken comment = new Token(new TextAttribute(manager.getColor(IJSONColorConstants.JSON_COMMENT)));

		IRule[] rules = new IRule[6];

		// Add rule for double quotes
		rules[0] = new SingleLineRule("\"", "\"", string, '\\');
		// Add a rule for single quotes
		rules[1] = new SingleLineRule("'", "'", string, '\\');
		// Add generic whitespace rule.
		rules[2] = new WhitespaceRule(new JSONWhitespaceDetector());
		
		rules[3] = new MultiLineRule("/*", "*/", comment);
		rules[4] = new EndOfLineRule("//", comment);
		
		WordRule jsonConstantsRule = new WordRule(new JSONConstantDetector(), Token.UNDEFINED);
		jsonConstantsRule.addWord("true", constant);
		jsonConstantsRule.addWord("false", constant);
		jsonConstantsRule.addWord("null", constant);
		rules[5] = jsonConstantsRule;
		
		setRules(rules);
	}
}
